<?php

/**
* 随机换头像
* by torn
* 13.10.3
*/

define('MAXNUM', 1612);
$url = 'http://' . $_SERVER['SERVER_NAME'] . '/pic/head/';//
$head = getImgName();
$pic = new imgdata($head);

/* 随机/指定图片 */
function getImgName() {
	$_num = intval($_GET['h']);
	$num = $_num > 0 && $_num <= MAXNUM ? $_num : rand(1, MAXNUM);
	$head = $num . '.jpg';
	file_exists($head) || $head = '000.jpg';
	return $head;
}
####### 图片获取&输出 类
class imgdata{
    public $imgsrc;
    public $imgdata;
    public $imgform;
    public function __construct($source){// 原 getdir()
            $this->imgsrc  = $source;
			//echo '<img src="'.$this->imgsrc.'">';
			### 一体化
			$this->img2data();
			$this->data2img();				
    }
    public function img2data(){// 将图片数据读入
            $this->_imgfrom($this->imgsrc);// 取得文件类型 type
            //return $this->imgdata=fread(fopen($this->imgsrc,'rb'),filesize($this->imgsrc));
			return $this->imgdata=file_get_contents($this->imgsrc);
    }
    public function data2img(){// 取出图片数据并输出浏览器
            $_GET['swt'] && file_put_contents("000.jpg",$this->imgdata);
            header("content-type:".$this->imgform);
            echo $this->imgdata;
            //echo $this->imgform;
            //imagecreatefromstring($this->imgdata);
    }
    public function _imgfrom($imgsrc){
            $info=getimagesize($imgsrc);
            //var_dump($info);
            return $this->imgform = $info['mime'];
    }
}
?>